// pages/buy/BuyPage/BuyPage.js
import WxValidate from '../../../utils/WxValidate.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    sellername:"",
    desc:"",
    price:"",
    goodsimg:"",
    avatarUrl:"",
    nickName:"",
    form: {
      dormitory: '',
      phone: '',
      trade:''
    },
    items: [
      { name: 'face', value: '面交', checked: 'true' },
    ]
  },
  onLoad() {
    this.getuser()
    this.initValidate()//验证规则函数
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    rules: { }
    messages: { }
    this.setData({
      id: options.id,
    })
  },
  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  //验证函数
  initValidate() {
    const rules = {
      dormitory: {
        required: true,
        minlength: 2
      },
      phone: {
        required: true,
        tel: true
      },
      trade: {
        required: true,
        minlength: 2
      }
    }
    const messages = {
      dormitory: {
        required: '请填写宿舍号',
        minlength: '请输入正确的宿舍号'
      },
      phone: {
        required: '请填写手机号',
        tel: '请填写正确的手机号'
      },
      trade: {
        required: '请填写交易方式',
        minlength: '请输入面交'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  //调用验证函数
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带的数据为：', e.detail.value)
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }
    this.showModal({
      msg: '提交成功'
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    var appsessionKey = app.globalData.sessionKey
    console.log(appsessionKey)
    if (!appsessionKey){
      wx.showModal({
        title: '您还未登录！',
        content: '请问是否前往登录？',
        showCancel: true,//是否显示取消按钮
        success: function (res) {

        },
        complete: function (res) {
          if (res.cancel) {
            //点击取消,默认隐藏弹框
            console.log("取消")
          } else {
            console.log("确定")
            //点击确定
            wx.switchTab({
              url: '../../mine/mine',
            })
          }
        }
      })
      
    }else{
      this.detailsPage()
    }
    
  },
  //获得订单信息
  detailsPage: function () {
    var a = this.data.id
    console.log(a)
    var that = this;
    wx.request({
      url: 'https://www.gxjyxq.cn/item/detail',
      data: {
        itemId: a,
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          price: res.data.price,
          desc: res.data.desc,
          goodsimg: res.data.cover,
        })
      }
    })
    wx.getUserInfo({
      success: function (res) {

        console.log(res.userInfo)
        that.setData({
          avatarUrl: res.userInfo.avatarUrl,
          nickName: res.userInfo.nickName
        })

        // console.log(res.userInfo)
      }
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
